package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.检查参数
        if(dto==null)return errorParam;
        return getPageData(dto, Wrappers.<AdSensitive>lambdaQuery().like(StringUtils.isNotEmpty(dto.getName()),AdSensitive::getSensitives,dto.getName()));
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数检查
        if(adSensitive == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);


        //2.保存
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return success;
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.参数检查
        if(adSensitive == null || adSensitive.getId() == null)return errorParam;

        //2.修改
        updateById(adSensitive);
        return success;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.参数检查
        if(id == null)return errorParam;
        //2.判断当前敏感词是否存在
        AdSensitive adSensitive = getById(id);
        if(adSensitive == null)return noData;
        //3.删除操作
        removeById(id);
        return success;
    }
}